package taglib;

import hibernate.Page;

import java.io.IOException;
import java.util.List;

import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.BodyTagSupport;

import org.apache.commons.lang.StringUtils;

import utils.StringHelper;

import config.Constant;

@SuppressWarnings("serial")
public class PageTag extends BodyTagSupport {
	
	private String pageKey = "page";
	private String formName = "frm";
	private String styleClass = "pagelink";
	private String params = "";
	private String url;
	private String ajax = Constant.FALSE;
	private String ajaxTarget;
	public int doStartTag() {
		 return (SKIP_BODY);
		
	}

	public int doEndTag() {
		
		String previous = getMessage("JspUtils.Paging.previous");
		String next = getMessage("JspUtils.Paging.paging.next");
		String first = getMessage("JspUtils.Paging.paging.first");
		String last = getMessage("JspUtils.Paging.paging.last");
		String change = getMessage("JspUtils.Paging.paging.change");
		String page = getMessage("JspUtils.Paging.paging.page");
		String total = getMessage("JspUtils.Paging.total");
		String records = getMessage("JspUtils.Paging.records");
		
		
		StringBuffer sbTR = new StringBuffer();
		Page pager = (Page)pageContext.getRequest(). getAttribute(pageKey);
		List items = pager.getItems();
		sbTR.append(">> ");
		if (items != null) {
			
			if(!pager.hasPrePage()){
				sbTR.append(first);
			}
			else{
				String url = buildUrl(pager,0);
				String pageStr = "";
				if(StringHelper.parseBoolean(ajax)){
					pageStr = "getPaginationData('"+url+"','"+ajaxTarget+"')";
				}
				else{
					pageStr = "document.forms."+formName+".action='"+url+"';document.forms."+formName+".submit();return false";
				}
				sbTR.append("<a onclick=\""+pageStr+"\" href=\"#\" class=\""+styleClass+"\">" + first + "</a>");
			}
			sbTR.append("&nbsp;&nbsp;");
			
			if (!pager.hasPrePage()) {
				sbTR.append(previous);
			} else {
				
				String url = buildUrl(pager,(pager.getPrePage() - 1) * pager.getLimit());
				String pageStr = "";
				if(StringHelper.parseBoolean(ajax)){
					pageStr = "getPaginationData('"+url+"','"+ajaxTarget+"')";
				}
				else{
					pageStr = "document.forms."+formName+".action='"+url+"';document.forms."+formName+".submit();return false";
				}
				
				sbTR.append("<a onclick=\""+pageStr+"\" href=\"#\" class=\""+styleClass+"\">" + previous + "</a>");
					
			}
			sbTR.append("&nbsp;&nbsp;");
			
			if(!pager.hasNextPage()) {
				sbTR.append(next);
				sbTR.append("&nbsp;&nbsp;");
				sbTR.append(last);
			} else {
				
				String url = buildUrl(pager,(pager.getNextPage() - 1)* pager.getLimit());
				String pageStr = "";
				if(StringHelper.parseBoolean(ajax)){
					pageStr = "getPaginationData('"+url+"','"+ajaxTarget+"')";
				}
				else{
					pageStr = "document.forms."+formName+".action='"+url+"';document.forms."+formName+".submit();return false";
				}
				
				sbTR.append("<a onclick=\""+pageStr+"\" href=\"#\" class=\""+styleClass+"\">" + next + "</a>");
				sbTR.append("&nbsp;&nbsp;");
				
				
				//
				url = buildUrl(pager,(pager.getNextPage() - 1)* pager.getLimit());
				pageStr = "";
				if(StringHelper.parseBoolean(ajax)){
					pageStr = "getPaginationData('"+url+"','"+ajaxTarget+"')";
				}
				else{
					pageStr = "document.forms."+formName+".action='"+url+"';document.forms."+formName+".submit();return false";
				}
				
				sbTR.append("<a onclick=\""+pageStr+"\" href=\"#\" class=\""+styleClass+"\">" + last + "</a>");
				
			}
		}
		
		if(pager.getTotalPages()>0){
			sbTR.append("&nbsp;&nbsp;");
			sbTR.append(total + pager.getTotalPages() + page + pager.getTotal() + records);
		}
		
		sbTR.append("&nbsp;&nbsp;");
		if(pager.getTotalPages()>1){
			sbTR.append(change);
			String tmpUrl = buildUrl(pager,-1);
			
			
			
			String pageStr = "";
			if(StringHelper.parseBoolean(ajax)){
				//"?start="+this.value
				pageStr = "getPaginationData('"+url+  ((tmpUrl.indexOf("?")>=0?"&":"?") + "start='+this.value")+ ",'"+ajaxTarget+"')";
				
			}
			else{
				pageStr = "document.forms."+formName+".action='"+tmpUrl+(tmpUrl.indexOf("?")>=0?"&":"?")+"start='+this.value"+";document.forms."+formName+".submit();return false;";
			}
			
			
			sbTR.append("<select onchange=\""+pageStr+"\">");
			for (int index = 1; index <= pager.getTotalPages(); index++) {
				sbTR.append("<option value=\""+(index -1)*pager.getLimit() +"\" ");
				
				if(pager.getStart()==(index -1)*pager.getLimit())
					sbTR.append(" selected=\"true\"");
				sbTR.append(">");
				sbTR.append(index);
				sbTR.append("</option>");
			}
			
			sbTR.append("</select>");
			sbTR.append(page);
		}
		try {
			JspWriter writer = pageContext.getOut();
			writer.write(sbTR.toString());
		} catch (IOException e) {
			e.printStackTrace();
		}

		return (SKIP_BODY);

	}
	
	public String buildUrl(Page page,int start){
		String tempURL = "";
		if(start>0){
			tempURL = appendToUrl(url,"start",String.valueOf(start));
		}
		else{
			tempURL = url;
		}
		
		
		

		if(StringUtils.isNotEmpty(params)){
			tempURL = tempURL + ( tempURL.indexOf("?") == -1 ? "?" :"");
			tempURL = tempURL + ( tempURL.endsWith("?") ? params: ("&" + params));
		}
		return tempURL;
	}
	
	
	public String buildUrl(Page paginationSupport){
		int start = paginationSupport.getStart()== 0 ? 1:paginationSupport.getStart();
		String tempURL = appendToUrl(url,"start",String.valueOf(start));
		
		if(StringUtils.isNotEmpty(params)){
			tempURL = tempURL + ( tempURL.indexOf("?") == -1 ? "?" :"");
			tempURL = tempURL + ( tempURL.endsWith("?") ? params: ("&" + params));
		}
		return tempURL;
	}
	
	
	public String getMessage(String key,Object... params ){
		return Constant.MESSAGE.getMessage(key, params, null);
	}
	
	public String appendToUrl(String src,String paramName,String paramValue){
		if(StringUtils.isNotEmpty(paramValue)&&StringUtils.isNotEmpty(paramName)){
			src = src + ( src.indexOf("?") == -1 ? "?" :"");
			src = src + ( src.endsWith("?") ? paramName+"="+paramValue :"&"+paramName+"="+paramValue);
		}
		return src;
	}

	public String getFormName() {
		return formName;
	}

	public void setFormName(String formName) {
		this.formName = formName;
	}

	public String getPageKey() {
		return pageKey;
	}

	public void setPageKey(String pageKey) {
		this.pageKey = pageKey;
	}

	public String getParams() {
		return params;
	}

	public void setParams(String params) {
		this.params = params;
	}

	public String getStyleClass() {
		return styleClass;
	}

	public void setStyleClass(String styleClass) {
		this.styleClass = styleClass;
	}

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}


	public String getAjaxTarget() {
		return ajaxTarget;
	}

	public void setAjaxTarget(String ajaxTarget) {
		this.ajaxTarget = ajaxTarget;
	}

	public String getAjax() {
		return ajax;
	}

	public void setAjax(String ajax) {
		this.ajax = ajax;
	}
}
